package mianyang.zhangsan.com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.pojo.BookOrder;
import mianyang.zhangsan.com.pojo.RoomType;
import mianyang.zhangsan.com.service.RoomTypeService;
import mianyang.zhangsan.com.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/roomType")
public class RoomTypeController {

    @Autowired
    private RoomTypeService roomTypeService;

    /**
     * 前端查询房间信息
     * @param pageNo
     * @param model
     * @return
     */
    @RequestMapping("/frontList")
    public String frontList(@RequestParam("pageNo")int pageNo, Model model){
        Page<RoomType> roomTypePage = roomTypeService.queryPages(pageNo);
        model.addAttribute("roomTypeList", roomTypePage.getRecords());
        roomTypePage.setRecords(null);
        model.addAttribute("page", roomTypePage);
        model.addAttribute("pages", roomTypePage.getPages());
        return "room/roomType_front_result";
    }

    /**
     * 前端添加房间数据
     * @return
     */
    @RequestMapping("/frontAdd")
    public String frontAdd(){

        return "room/roomType_frontAdd";
    }

    /**
     * 后台跳转添加房型页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toRoomTypeAdd(){
        return "room/roomType_add";
    }

    /**
     * 后台添加房间类型
     * @param roomType
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public String roomTypeAdd(RoomType roomType){
        System.out.println(roomType);
        if (roomType != null){
            RoomType check = roomTypeService.queryRoomTypeInfo(roomType.getRoomTypeName());
            if (check != null){
                return "{\"success\": false,\"message\": \"房间类型已经存在\"}";
            }
        }
        roomTypeService.addRoomType(roomType);
        return  "{\"success\": true}";
    }

    /**
     * 查询所有房间类型
     * @return
     */
    @RequestMapping(value="/listAll", produces="application/json;charset=utf-8")
    @ResponseBody
    public String roomTypeList(){
        List<RoomType> roomTypes = roomTypeService.queryAll();
        JSONArray response = new JSONArray();
        for (RoomType roomType: roomTypes){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("roomTypeId", roomType.getRoomTypeId());
            jsonObject.put("roomTypeName", roomType.getRoomTypeName());
            response.add(jsonObject);
        }
        return response.toString();
    }

    /**
     * 后台管理房型
     * @return
     */
    @RequestMapping("/query")
    public String roomTypeQuery(){
        return "room/roomType_query_result";
    }

    /**
     * 后台查询房间类型信息
     * @param roomType
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value ="/list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryList(RoomType roomType, Integer page, Integer rows){
        System.out.println(roomType);
        Page<RoomType> roomTypePage = roomTypeService.queryPageByWrapper(page, rows, roomType);
        JSONObject json = new JSONObject();
        json.put("total", roomTypePage.getPages());
        JSONArray jsonArray = new JSONArray();
        for (RoomType item : roomTypePage.getRecords()){
            Object o = JSON.toJSON(item);
            jsonArray.add(o);
        }
        json.put("rows", jsonArray);

        return json.toString();
    }

    /**
     * 后台导出新闻信息
     * @param roomType
     * @param response
     */
    @RequestMapping("/outToExcel")
    public void toExcel(RoomType roomType, HttpServletResponse response){
        List<RoomType> roomTypes = roomTypeService.queryListByWrapper(roomType);
        String[] headers = {"序号", "房间类型",  "价格"};
        String title = "房间类型表";
        List<String[]> contents = new ArrayList<>();
        for (RoomType item : roomTypes){
            contents.add(new String[]{item.getRoomTypeId().toString(),item.getRoomTypeName(),item.getPrice().toString()});
        }
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setHeader("Content-disposition","attachment; filename="+"roomType.xls");//filename是下载的xls的名，建议最好用英文
            response.setContentType("application/msexcel;charset=UTF-8");//设置类型
            response.setHeader("Pragma","No-cache");//设置头
            response.setHeader("Cache-Control","no-cache");//设置头
            response.setDateHeader("Expires", 0);//设置日期头
            ExcelUtils.exportExcel(title, headers, contents, out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if (out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
